/*
 * @(#)MeetingMessageFormatter.java 
 * Copyright (c) 2006 DCIVision Ltd All rights reserved.
 * 
 *  This software is the confidential and proprietary information of DCIVision Ltd 
 *  ("Confidential Information"). You * shall not disclose such Confidential
 *   Information and shall use it only in accordance with the terms of the 
 *   license agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.alert.core;

import java.sql.Connection;
import java.text.SimpleDateFormat;

import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.alert.bean.UpdateAlertSystemLog;
import com.dcivision.alert.bean.UpdateAlertType;
import com.dcivision.alert.dao.UpdateAlertDAObject;
import com.dcivision.alert.dao.UpdateAlertTypeDAObject;
import com.dcivision.calendar.bean.CalendarRecord;
import com.dcivision.calendar.dao.CalendarRecordDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;

/**
 * <p>
 * Class Name: MeetingMessageFormatter
 * </p>
 * 
 * @author Greatwall shao
 * @company DCIVision Limited
 * @creation date 2006-11-06
 */
public class MeetingMessageFormatter extends AbstractMessageFormatter {

  private UpdateAlertTypeDAObject updateAlertTypeDAO;

  private UpdateAlertType         updateAlertType;

  public MeetingMessageFormatter(UpdateAlertSystemLog bean, SessionContainer sessionContainer,
      Connection dbConn) throws ApplicationException {
    super(bean, sessionContainer, dbConn);
  }

  protected void makeMessageType() {
    if (UpdateAlert.INVITE_TYPE.equals(getActionType())
        && getUpdateAlertSystemLog().getCreatorID().intValue() != getSessionContainer()
            .getUserRecordID().intValue()) {

      setMessageType("meetingInvitation");

    }
    else {

      setMessageType("meetingNotification");
    }
  }

  public String getSender() {
    String str = "";
    if (!UpdateAlert.INVITE_TYPE.equals(updateAlertSystemLog.getActionType())) {
      str = "System";
    }
    else {
      str = userName;
    }
    return str;
  }

  private void makeMeeting() throws ApplicationException {
    String objectType = getObjectType();
    String actionType = getActionType();
    UpdateAlert updateAlert;
    UpdateAlertDAObject updateAlertDAO = new UpdateAlertDAObject(sessionContainer, dbConn);
    updateAlert = (UpdateAlert) updateAlertDAO.getObjectByID(updateAlertSystemLog
        .getUpdateAlertID());
    CalendarRecordDAObject calendarRecordDAO = new CalendarRecordDAObject(sessionContainer, dbConn);
    CalendarRecord calendarRecord = (CalendarRecord) calendarRecordDAO
        .getOriCalendarByID(updateAlert.getObjectID());
    String meetingTitle = calendarRecord.getTitle();
    if ((UpdateAlert.INVITE_TYPE.equals(actionType) && (calendarRecord.getCreatorID().intValue() == sessionContainer
        .getUserRecordID().intValue()))
        || UpdateAlert.REMIND_ACTION.equals(actionType)) {
      String dueDate = "";
      if (null != this.updateAlertSystemLog.getDueDate()) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM hh:mm", sessionContainer
            .getSessionLocale());
        dueDate = formatter.format(this.updateAlertSystemLog.getDueDate());
      }
      this.messageSubject = getMessageResource("home.message.meeting_notification.remind",
          meetingTitle, userName, dueDate);

    }
    else if (UpdateAlert.INVITE_TYPE.equals(actionType)) {

      this.messageSubject = getMessageResource("home.message.meeting_invitation", meetingTitle);

    }
    else {

      if (UpdateAlert.CALENDAR_MODIFY_TYPE.equals(objectType)) {

        this.messageSubject = getMessageResource("home.message.meeting_notification.change",
            meetingTitle, userName);
      }
      else if (UpdateAlert.CALENDAR_INACTIVE_TYPE.equals(objectType)) {

        this.messageSubject = getMessageResource("home.message.meeting_notification.cancel",
            meetingTitle, userName);
      }
    }
  }

  protected void setUserDefinedInviteSubject() {
    this.messageSubject = this.updateAlertType.getAlertSubject();
    if (UpdateAlert.CALENDAR_MODIFY_TYPE.equals(updateAlertSystemLog.getObjectType())) {
      this.messageSubject = this.getMessageResource("home.message.meeting_change") + " "
          + this.messageSubject;
    }
  }

  protected void setSystemMakeSubject() throws ApplicationException {
    this.updateAlertTypeDAO = new UpdateAlertTypeDAObject(sessionContainer, dbConn);
    this.updateAlertType = (UpdateAlertType) updateAlertTypeDAO
        .getObjectByID(this.updateAlertSystemLog.getUpdateAlertTypeID());

    if (UpdateAlert.INVITE_ACTION.equals(getActionType())
        && !Utility.isEmpty(this.updateAlertType.getAlertSubject())) {
      setUserDefinedInviteSubject();
    }
    else {
      makeMeeting();
    }
  }


}
